package com.kbbywyclient.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.kbbywyclient.R;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.entity.Banner;
import com.kbbywyclient.data.entity.HotRec;
import com.kbbywyclient.data.entity.IndexData;
import com.kbbywyclient.data.model.BannerModel;
import com.kbbywyclient.data.model.HotReModel;
import com.kbbywyclient.data.model.IndexDataModel;
import com.kbbywyclient.data.model.base.BaseModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.IndexContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Observer;
import rx.Subscriber;
/**
 * Created by Jiangzx on 2017/8/17 11:05.
 */

public class IndexPresenter extends BasePresenter implements IndexContract.Presenter {
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil jsonUtil;

    public static final int START_PAGE = 0;
    private IndexContract.View view;
    public int pageNo = START_PAGE;
    private List<HotRec> hotRecs;

    private boolean isLoadingRedPacket;

    @Inject
    public IndexPresenter(IndexContract.View view) {
        this.view = view;
        hotRecs = new ArrayList<>();
    }

    @Override
    public void getIndexData(String saleId) {
        jsonUtil.put("parent_code", Constants.JBQ011101);
        if (!TextUtils.isEmpty(saleId)) {
            jsonUtil.put("saleID", saleId);
        }
        ApiServiceHelper.appointThreadNormal(apiService.getIndexData(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<IndexDataModel>() {
                    @Override
                    public void onCompleted() {
                        view.finishRefresh();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.finishRefresh();
                        view.onIndexDataFailure();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(IndexDataModel indexDataModel) {
                        IndexData data = indexDataModel.getData();
                        if (data != null) {
                            view.onIndexDataSuccess(data);
                        } else {
                            view.onIndexDataFailure();
                        }
                    }
                });
    }

    @Override
    public void getBannerData(String code) {
        jsonUtil.put("banner_code", code);
        ApiServiceHelper.appointThreadNormal(apiService.getBanner(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<BannerModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.hideBanner();
                    }

                    @Override
                    public void onNext(BannerModel bannerModel) {
                        Banner banner = bannerModel.getData();
                        if (banner != null) {
                            List<Banner.Bean> list = banner.getCarousel_figure_list();
                            if (list != null && list.size() > 0) {
                                view.onBannerSuccess(list);
                            } else {
                                view.hideBanner();
                            }
                        } else {
                            view.hideBanner();
                        }
                    }
                });
    }

    @Override
    public void getRedPacket(String code) {
        if (isLoadingRedPacket) {
            return;
        }
        isLoadingRedPacket = true;
        jsonUtil.put("banner_code", code);
        ApiServiceHelper.appointThreadNormal(apiService.getBanner(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<BannerModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        isLoadingRedPacket = false;
                    }

                    @Override
                    public void onNext(BannerModel bannerModel) {
                        Banner banner = bannerModel.getData();
                        if (banner != null) {
                            List<Banner.Bean> list = banner.getCarousel_figure_list();
                            if (list != null && list.size() > 0) {
                                view.onRedPacketSuccess(list);
                            } else {
                                view.hideBanner();
                            }
                        } else {
                            view.hideBanner();
                        }
                        isLoadingRedPacket = false;
                    }
                });
    }

    @Override
    public void getHotRecList(String saleId, int page) {
        jsonUtil.put("Code", Constants.DCP021701);
        if (!TextUtils.isEmpty(saleId)) {
            jsonUtil.put("saleID", saleId);
        }
        jsonUtil.put("pageNum", page);

        ApiServiceHelper.
                appointThreadNormal(apiService.getHotRec(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<HotReModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        pageNo--;
                        if (pageNo < START_PAGE) {
                            pageNo = START_PAGE;
                        }
                        view.loadListEmpty();
                    }

                    @Override
                    public void onNext(HotReModel model) {
                        List<HotRec> data = model.getData();
                        if (data == null) {
                            view.loadListEmpty();
                        } else {
                            if (page == START_PAGE) {
                                hotRecs.clear();
                            }
                            if (data.size() > 0) {
                                hotRecs.addAll(data);
                            }
                            if (page == model.getTotal_page() - 1) {
                                //没有更多数据了
                                view.setListUi(hotRecs, false);
                            } else {
                                view.setListUi(hotRecs, true);
                            }
                        }
                    }
                });
    }

    @Override
    public void clearList() {
        pageNo = START_PAGE;
        if (hotRecs != null) {
            hotRecs.clear();
        }
    }

    @Override
    public void updateSaleBalance(String saleID) {
        jsonUtil.put("saleID", saleID);
        view.showLoadingDialog(mContext.getString(R.string.text_loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.updateSaleBlance(jsonUtil.getRsaParams())).
                subscribe(new Observer<BaseModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(BaseModel baseModel) {
                        if (baseModel.getResult().equals(Constants.SUCCESS)) {
                            view.loadSucUpdate(baseModel);
                        } else {
                            ToastUtil.showShort(baseModel.getResult());
                        }
                    }
                });
    }
}
